package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.po.Course;
import com.study.happystudy.service.impl.CourseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("course/")
public class CourseController {
    @Resource
    CourseServiceImpl courseService;

    /**
     * 新建课程
     *
     * @return 返回课程id
     */
    @PostMapping("insertCourse")
    public String insertCourse(
            @RequestParam("teacherId") int userId,
            @RequestParam("courseName") String courseName,
            @RequestParam("courseDescription") String courseDescription,
            @RequestParam("imgUrl") String imgUrl,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("duration") int duration,
            @RequestParam("grade") int grade,
            @RequestParam("subjectId") int subjectId,
            @RequestParam("classTime") String classTime,
            @RequestParam String type
    ) {
        boolean isPooh;
        try {
            isPooh = courseService.insertCourse(userId, courseName, courseDescription, imgUrl, startTime, endTime, duration, grade, subjectId, classTime,type);
        } catch (Exception e) {
            return ResultEntity.Fail(-2, "失败了");
        }
        if (isPooh) return ResultEntity.Success(1, "课程添加成功");
        else return ResultEntity.Fail(-1, "课程添加失败");
    }

    /**
     * 根据id删除课程
     */
    @DeleteMapping("deleteCourseById")
    public String deleteCourseById(@RequestParam("courseId") int courseId) {
        boolean success = courseService.deleteCourseById(courseId);
        if (success) {
            return ResultEntity.Success("课程删除成功");
        } else {
            return ResultEntity.Fail(-1, "不存在该课程");
        }
    }

    /**
     * 更新课程信息
     */
    @PostMapping("updateCourse")
    public String updateCourse(
            @RequestParam("courseId") int courseId,
            @RequestParam("courseName") String courseName,
            @RequestParam("courseDescription") String courseDescription,
            @RequestParam("imgUrl") String imgUrl,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("duration") int duration,
            @RequestParam("grade") int grade,
            @RequestParam("subjectId") int subjectId,
            @RequestParam("classTime") String classTime,
            @RequestParam String type
    ) {
        boolean success = courseService.updateCourse(courseId, courseName, courseDescription,
                imgUrl, startTime, endTime, duration, grade, subjectId, classTime,type);
        if (success) {
            return ResultEntity.Success("课程更新成功");
        } else {
            return ResultEntity.Fail(-1, "不存在该课程");
        }
    }

    /**
     * 查询报名该课程的学生(按照加入日期排序)
     */
    @GetMapping("selectStudentsByCourseId")
    public Object selectStudentsByCourseId(@RequestParam("courseId") int courseId) {
        try {
            return courseService.selectStudentsByCourseId(courseId);
        } catch (Exception e) {
            return ResultEntity.Fail(-1, "Fail because Pooh is dick:\n" + e.getMessage());
        }
    }

    /**
     * 增加学生到课程
     *
     * @return 0代表不存在学生与课程,-1代表找不到课程,-2代表找不到学生,-3代表该学生已经加入该课程
     */
    @PostMapping("insertStudentToCourse")
    public String insertStudentToCourse(
            @RequestParam("courseId") int courseId,
            @RequestParam("userId") int userId
    ) {

        int result = courseService.insertStudentToCourse(courseId, userId);
        if (result == 1) {
            return ResultEntity.Success(1, "学生添加成功");
        } else {
            return ResultEntity.Fail(result, "学生添加失败");
        }
    }

    /**
     * 删除课程中的学生
     *
     * @return 0代表不存在学生与课程,-1代表找不到课程,-2代表找不到学生,-3代表该学生未加入该课程
     */
    @DeleteMapping("deleteStudentFromCourse")
    public String deleteStudentFromCourse(
            @RequestParam("courseId") int courseId,
            @RequestParam("userId") int userId
    ) {
        int result = courseService.deleteStudentFromCourse(courseId, userId);
        if (result == 1) {
            return ResultEntity.Success(1, "学生删除成功");
        } else {
            return ResultEntity.Fail(result, "学生删除失败");
        }
    }

    // 搜索框
    @GetMapping("selectCourses")
    public List<Course> selectCourses(@RequestParam("grade") Integer grade, @RequestParam("subjectId") Integer subjectId) {
//        ColorChange.BlueGreen("grade: "+grade+" subjectId: "+subjectId);

        if (grade == -1 && subjectId == -1) {
            return courseService.getAllCourses();
        }

        if (grade != -1 && subjectId != -1) {
            return courseService.selectCoursesByGradeAndSubject(grade, subjectId);
        }

        if (grade != -1) {
            return courseService.selectCoursesByGrade(grade);
        }

        // grade == -1
        return courseService.selectCoursesBySubject(subjectId);
    }

    /**
     * 根据科目返回课程列表(开课时间排序)
     */
    @GetMapping("selectCoursesBySubject")
    public List<Course> selectCoursesBySubject(@RequestParam("subjectId") int subjectId) {
        return courseService.selectCoursesBySubject(subjectId);
    }

    /**
     * 根据年级和科目返回课程列表(开课时间排序)
     */
    @GetMapping("selectCoursesByGradeAndSubject")
    public List<Course> selectCoursesByGradeAndSubject(
            @RequestParam("grade") int grade,
            @RequestParam("subjectId") int subjectId
    ) {
        return courseService.selectCoursesByGradeAndSubject(grade, subjectId);
    }

    /**
     * 根据年级返回课程列表(开课时间排序)
     */
    @GetMapping("selectCoursesByGrade")
    public List<Course> selectCoursesByGrade(@RequestParam("grade") int grade) {
        return courseService.selectCoursesByGrade(grade);
    }

    /**
     * 搜索包含关键词的课程
     * 课程名称,老师昵称,课程简介等...
     */
    @GetMapping("searchByTerm")
    public List<Course> searchByTerm(@RequestParam("searchTerm") String searchTerm) {
        // 先匹配老师姓名
        List<Course> teacher = courseService.searchCourseByTeacherName(searchTerm);
        // 如果是查询的关键词能够完全匹配老师昵称(不唯一),则一定是找该老师的课,返回该老师教授的所有课程
        if (!teacher.isEmpty()) return teacher;
        // 匹配剩余项
        return courseService.searchByTerm(searchTerm);
    }

    @PostMapping("collectCourses")
    public String collectCourses(@RequestParam("userId") int userId, @RequestParam("courseId") int courseId) {

        int result = courseService.isUserOrCourseExist(userId, courseId);

        if (result != 1) {
            return ResultEntity.Fail(result, "用户或课程不存在");
        }

        // 如果已经收藏则取消收藏
        if (courseService.isFavorites(userId, courseId)) {
            courseService.cancelCollectCourses(userId, courseId);
            return ResultEntity.Success(1, "取消成功");
        }

        courseService.collectCourses(userId, courseId);
        return ResultEntity.Success(1, "收藏成功");
    }

    @GetMapping("getUserFavorites")
    public List<Course> getUserFavorites(@RequestParam("userId") int userId, @RequestParam("subjectId") int subjectId, @RequestParam("grade") int grade) {
        if(subjectId == -1 && grade == -1){
            return courseService.getUserFavorites(userId);
        }
        if(subjectId == -1){
            courseService.getUserFavoritesByGrade(userId,grade);
        }
        if(grade == -1){
            return courseService.getUserFavoritesBySubject(userId,subjectId);
        }
        return courseService.getUserFavoritesBySubjectAndGrade(userId,subjectId,grade);
    }

    @GetMapping("isFavorites")
    public String isFavorites(@RequestParam("userId") int userId, @RequestParam("courseId") int courseId) {

        int result = courseService.isUserOrCourseExist(userId, courseId);

        if (result != 1) {
            return ResultEntity.Fail(result, "用户或课程不存在");
        }

        // 判断是否收藏
        if (courseService.isFavorites(userId, courseId)) {
            return ResultEntity.Success(true, "已收藏");
        }

        return ResultEntity.Success(false, "未收藏");
    }

    @GetMapping("findCourseById")
    public Course findCourseById(@RequestParam("courseId") int courseId) {
        return courseService.findCourseById(courseId);
    }

    @GetMapping("getAll")
    public List<Course> getAllCourses() {
        return courseService.getAllCourses();
    }

    @GetMapping("selectCoursesByUserId")
    public List<Course> selectCoursesByUserId(@RequestParam("userId") int userId) {
        return courseService.selectCoursesByUserId(userId);
    }
}
